<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'srm:cw:billvouch:puGeneral:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="80%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsSrmCwBillvouchPuGeneralForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { PuGeneral, puGeneralSave, puGeneralForm } from '/@/api/srm/cw/billvouch/puGeneral';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('srm.cw.billvouch.puGeneral');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<PuGeneral>({} as PuGeneral);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增待开票区') : t('编辑待开票区'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('入库单号'),
      field: 'vbillcode',
      component: 'Input',
      componentProps: {
        maxlength: 30,
      },
    },
    {
      label: t('入库日期'),
      field: 'dbilldate',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
    },
    {
      label: t('仓库'),
      field: 'wareName',
      component: 'Input',
      componentProps: {
        maxlength: 400,
      },
    },
    {
      label: t('流程名称'),
      field: 'busTypeName',
      component: 'Input',
      componentProps: {
        maxlength: 2048,
      },
    },
    {
      label: t('部门'),
      field: 'cgDeptName',
      component: 'Input',
      componentProps: {
        maxlength: 800,
      },
    },
    {
      label: t('供应商'),
      field: 'venName',
      component: 'Input',
      componentProps: {
        maxlength: 400,
      },
    },
    {
      label: t('备注'),
      field: 'vnote',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 181,
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('订单号'),
      field: 'orerCode',
      component: 'Input',
      componentProps: {
        maxlength: 30,
      },
    },
    {
      label: t('订单日期'),
      field: 'orderDate',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
    },
    {
      label: t('行号'),
      field: 'crowno',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('存货编码'),
      field: 'cinvcode',
      component: 'Input',
      componentProps: {
        maxlength: 160,
      },
    },
    {
      label: t('存货名称'),
      field: 'cinvname',
      component: 'Input',
      componentProps: {
        maxlength: 400,
      },
    },
    {
      label: t('规格'),
      field: 'cinvspec',
      component: 'Input',
      componentProps: {
        maxlength: 400,
      },
    },
    {
      label: t('型号'),
      field: 'cinvtype',
      component: 'Input',
      componentProps: {
        maxlength: 400,
      },
    },
    {
      label: t('单位名称'),
      field: 'measname',
      component: 'Input',
      componentProps: {
        maxlength: 400,
      },
    },
    {
      label: t('批次号'),
      field: 'cbatch',
      component: 'Input',
      componentProps: {
        maxlength: 30,
      },
    },
    {
      label: t('入库数量'),
      field: 'ninnum',
      component: 'Input',
      componentProps: {
        maxlength: 18,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('无税单价'),
      field: 'nprice',
      component: 'Input',
      componentProps: {
        maxlength: 18,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('无税金额'),
      field: 'nmny',
      component: 'Input',
      componentProps: {
        maxlength: 18,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('含税单价'),
      field: 'ntaxprice',
      component: 'Input',
      componentProps: {
        maxlength: 18,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('含税金额'),
      field: 'ntaxmny',
      component: 'Input',
      componentProps: {
        maxlength: 18,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('税率'),
      field: 'nictaxrate',
      component: 'Input',
      componentProps: {
        maxlength: 18,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('税额'),
      field: 'noritaxmny',
      component: 'Input',
      componentProps: {
        maxlength: 18,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('累计结算数量'),
      field: 'naccountnum1',
      component: 'Input',
      componentProps: {
        maxlength: 18,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('退货标志'),
      field: 'freplenishflag',
      component: 'Input',
      componentProps: {
        maxlength: 1,
      },
    },
    {
      label: t('币种'),
      field: 'currtypename',
      component: 'Input',
      componentProps: {
        maxlength: 800,
      },
    },
    {
      label: t('备注'),
      field: 'vnotebody',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 181,
      },
      colProps: { lg: 24, md: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 8, md: 24 },
    disabled: true,
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await puGeneralForm(data);
    record.value = (res.puGeneral || {}) as PuGeneral;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
        parentId: record.value.parentId,
        busType: record.value.busType,
        cgDeptId: record.value.cgDeptId,
        venId: record.value.venId,
        invId: record.value.invId,
        pkCorp: record.value.pkCorp,
      };
      // console.log('submit', params, data, record);
      const res = await puGeneralSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
